1208B - Uniqueness - CodeForces Solution


binary search brute force implementation two pointers *1500

Please click on ads to support us..

Python Code:


def works(x):
    curr = rep
    freq_ = freq.copy()
    for i in range(x):
        freq_[a[i]] -= 1
        if freq_[a[i]] == 1:
            curr -= 1

    if curr == 0:
        return True

    for i in range(x, n):
        freq_[a[i-x]] += 1
        if freq_[a[i-x]] == 2:
            curr += 1
        freq_[a[i]] -= 1
        if freq_[a[i]] == 1:
            curr -= 1
        if curr == 0:
            return True

    return False


n = int(input())

a = list(map(int, input().split()))

rep = 0
freq = {}
for i in range(n):
    if a[i] not in freq:
        freq[a[i]] = 0
    freq[a[i]] += 1
    if freq[a[i]]==2:
        rep += 1

left = 0
right = n
while left < right:
    mid = (left+right)//2
    if works(mid):
        right = mid
    else:
        left = mid+1

print(left)


C++ Code:

#include<bits/stdc++.h>
#include<queue>
#define ll long long
using namespace std;
const int mod=1e9+7;

int main(){
    

        ll n ;
        cin>>n;

        ll arr[n];
        for(ll i=0 ; i<n ; i++) cin>>arr[i] ;

        map<ll,ll> mp ;

        ll ans = INT_MAX ;

        for(ll i=0 ; i<n ; i++){

                if(mp[arr[i]] > 0) {
                            break ;
                }

                mp[arr[i]]++ ;

                map<ll,ll> mp2 = mp ;

                ll j = n-1 ;
                for(j=n-1 ; j>i ; j--){

                        if(mp2[arr[j]] > 0){
                            break ;
                        }

                        mp2[arr[j]]++;
                }

                ans = min(ans , j-i) ;
                
        }


        mp.clear() ;

        for(ll i=n-1 ; i>=0 ; i--){

                if(mp[arr[i]] > 0) {
                            break ;
                }

                mp[arr[i]]++ ;

                map<ll,ll> mp2 = mp ;

                ll j = 0 ;
                for(j=0 ; j < i ; j++){

                        if(mp2[arr[j]] > 0){
                            break ;
                        }

                        mp2[arr[j]]++;
                }

                ans = min(ans ,i-j) ;
                
        }

        cout<<ans<<endl;
       
}


Comments

Submit
0 Comments
More Questions

1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest
385A - Bear and Raspberry
1311B - WeirdSort
1713F - Lost Array
236B - Easy Number Challenge
275A - Lights Out
147A - Punctuation
253A - Boys and Girls
1327E - Count The Blocks
984A - Game
12B - Correct Solution
1355B - Young Explorers
485A - Factory
628A - Tennis Tournament
1436B - Prime Square
1707B - Difference Array
1422C - Bargain